package com.iflytek.autofly.setting.biz;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.iflytek.autofly.setting.R;
import com.iflytek.autofly.setting.biz.base.CommandHelper;
import com.iflytek.autofly.setting.biz.base.ICommand;
import com.iflytek.autofly.setting.biz.base.TabFragmentActicity;
import com.iflytek.autofly.setting.biz.car.CarFragment;
import com.iflytek.autofly.setting.biz.display.DisplayFragment;
import com.iflytek.autofly.setting.biz.more.MoreFragment;
import com.iflytek.autofly.setting.biz.navi.NaviFragment;
import com.iflytek.autofly.setting.biz.networking.NetworkingFragment;
import com.iflytek.autofly.setting.biz.receiver.FlySettingReceiver;
import com.iflytek.autofly.setting.biz.sound.SoundFragment;
import com.iflytek.autofly.setting.biz.system.SystemFragment;
import com.iflytek.autofly.setting.biz.voice.VoiceFragment;
import com.iflytek.autofly.utils.FlyLog;
import com.iflytek.sunflower.FlowerCollector;

import java.util.ArrayList;

/**
 * <b>Description: </b>主activity<br/>
 * <b>Change History:</b><br/>
 * <table border="1">
 * <tr>
 * <td><b>user</b></td>
 * <td><b>date</b></td>
 * <td><b>time</b></td>
 * <td><b>reason</b></td>
 * </tr>
 * <tr>
 * <td>JunLuan@iflytek.com</td>
 * <td>2015年5月26日</td>
 * <td>上午11:25:14</td>
 * <td>创建此文件</td>
 * </tr>
 * </table>
 *
 * @author JunLuan@iflytek.com
 */
public class MainActivity extends TabFragmentActicity implements ICommand {

    public static final String TAG = "MainActivity";
    private static final String[] CMD = {"network", "sound", "display", "navi", "voice", "system", "car", "more"};
    private int mAdvanceCount = 0;
    private boolean mReturnHome = true;
    private String mMagicString = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FlyLog.d(TAG, "command=" + FlySettingReceiver.ReturnConfig.cmd);
        excute(FlySettingReceiver.ReturnConfig.cmd);
        FlySettingReceiver.ReturnConfig.cmd = "";
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        FlyLog.d(TAG, "command=" + FlySettingReceiver.ReturnConfig.cmd);
        excute(FlySettingReceiver.ReturnConfig.cmd);
        FlySettingReceiver.ReturnConfig.cmd = "";
    }

    @Override
    protected void onResume() {
        super.onResume();
        FlyLog.d(TAG, "command=" + FlySettingReceiver.ReturnConfig.cmd);
        excute(FlySettingReceiver.ReturnConfig.cmd);
        FlySettingReceiver.ReturnConfig.cmd = "";
    }

    @Override
    protected String getAactivityName() {
        return getResources().getString(R.string.app_name);
    }

    @Override
    protected void setTabs(ArrayList<FragmentUnit> fragmentUnits) {
        fragmentUnits.add(new FragmentUnit(new NetworkingFragment(),
                getString(R.string.networking)));
        fragmentUnits.add(new FragmentUnit(new SoundFragment(),
                getString(R.string.sound)));
        fragmentUnits.add(new FragmentUnit(new DisplayFragment(),
                getString(R.string.display)));
        fragmentUnits.add(new FragmentUnit(new NaviFragment(),
                getString(R.string.navi)));
        fragmentUnits.add(new FragmentUnit(new VoiceFragment(),
                getString(R.string.iflyvoice)));
        fragmentUnits.add(new FragmentUnit(new SystemFragment(),
                getString(R.string.system)));
        fragmentUnits.add(new FragmentUnit(new CarFragment(),
                getString(R.string.car)));
//        fragmentUnits.add(new FragmentUnit(new MoreFragment(),
//                getString(R.string.more)));
        setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onClick(int position) {
                switch (position) {
                    case 0:
                        // 网络
                        mMagicString = "";
                        mAdvanceCount++;
                        if (mAdvanceCount == 7) {
                            mAdvanceCount = 0;
                            startActivity(new Intent(MainActivity.this, Secret2Activity.class));
                        }
                        break;
                    case 2:
                        // 显示
                        mAdvanceCount = 0;
                        if (mMagicString.equals("25")) {
                            mMagicString += "2";
                        } else {
                            mMagicString = "2";
                        }
                        break;
                    case 3:
                        // 导航
                        mAdvanceCount = 0;
                        if (mMagicString.equals("252")) {
                            mMagicString += "3";
                        } else {
                            mMagicString = "";
                        }
                        break;
                    case 5:
                        // 系统
                        mAdvanceCount = 0;
                        if (mMagicString.equals("2") || mMagicString.equals("2523")) {
                            mMagicString += "5";
                            if (mMagicString.equals("25235")) {
                                mMagicString = "";
                                startActivity(new Intent(MainActivity.this, SecretActivity.class));
                            }
                        } else {
                            mMagicString = "";
                        }
                        break;
                    default:
                        mAdvanceCount = 0;
                        mMagicString = "";
                }
            }
        });
    }


    @Override
    protected void init() {

    }

    @Override
    public void excute(String cmdline) {
        FlyLog.d(TAG, "cmd=" + cmdline);
        if (TextUtils.isEmpty(cmdline)) {
            return;
        }
        String[] cmds = new String[2];
        CommandHelper.getCommand(cmdline, cmds);
        if (TextUtils.isEmpty(cmds[0])) {
            return;
        }
        int index;
        for (index = CMD.length - 1; index >= 0; index--) {
            if (CMD[index].equals(cmds[0])) {
                break;
            }
        }
        if (index == -1) {
            return;
        }

        FlowerCollector.onEvent(getApplicationContext(), getLeftButtonName(index), "broadcast");
        switchToFragment(index);
        if (!TextUtils.isEmpty(cmds[1])) {
            ICommand command = getCommand(index);
            if (command != null) {
                command.excute(cmds[1]);
            }
        }
    }

    @Override
    public void onBackPressed() {
        if (FlySettingReceiver.ReturnConfig.returnHome) {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            startActivity(intent);
        } else {
            super.onBackPressed();
        }
        FlySettingReceiver.ReturnConfig.returnHome = true;
    }

    public void showMassiveMessage(String title, String content) {
        Intent intent = new Intent(this, MassiveMessageActivity.class);
        intent.putExtra(MassiveMessageActivity.TITLE, title);
        intent.putExtra(MassiveMessageActivity.CONTENT, content);
        startActivity(intent);
    }

    @Override
    protected void onPause() {
        super.onPause();
    }
}
